package com.bridgeintelligent.tag.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 获取系统配置
 *
 * @author zyd
 */
public class PropertityUtil {
    private static Log log = LogFactory.getLog(PropertityUtil.class);
    private static final Properties prop_sys = new Properties();

    public PropertityUtil(String proptName) {
        loadProperties(prop_sys, proptName);
    }

    /**
     * 将指定名称的properties文件加载到prop对象中
     *
     * @param prop
     * @param proptName
     * @return
     */
    private static Properties loadProperties(Properties prop, String proptName) {
        if (prop == null) {
            prop = new Properties();
        }
        try {
            InputStream is = PropertityUtil.class.getClassLoader().getResourceAsStream(proptName);
            if (is != null && is.available() > 0) {
                prop.load(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }

    /**
     * 根据key获取配置文件中的value
     *
     * @param key
     * @return
     */
    public static String srv(String key) {
        return prop_sys.getProperty(key);
    }

    /**
     * 获取指定属性文件的属性值
     *
     * @param propFileName
     * @param key
     * @return 属性值
     */
    public static String srv(String propFileName, String key) {
        String value = null;
        InputStream is = null;
        Properties prop = null;
        try {
            prop = new Properties();
            is = PropertityUtil.class.getClassLoader().getResourceAsStream(propFileName);
            if (is != null && is.available() > 0) {
                prop.load(is);
                value = prop.getProperty(key);
                prop = null;
            }
        } catch (IOException e) {
            log.error("获取指定属性文件的属性值异常:", e);
        }
        return value;
    }

    public static void main(String[] args) {
        System.out.println(PropertityUtil.srv("fdfs_client.conf", "temp_path"));
        System.out.println(PropertityUtil.srv("temp_path"));
    }
}
